//
//  STYLeftViewController.m
//  CarOfHome2
//
//  Created by dlios on 15/9/29.
//  Copyright (c) 2015年 Frank. All rights reserved.
//

#import "STYLeftViewController.h"
#import "CollectController.h"//收藏
#import "AboutUs.h"//关于我们

#import <SDImageCache.h>

#import "HomeViewController.h"
@interface STYLeftViewController ()

@property (nonatomic, strong) UIVisualEffectView *blur;
@property (nonatomic,strong) UIButton *night;
@end

@implementation STYLeftViewController
- (void)viewWillAppear:(BOOL)animated{
    AppDelegate *dele = [[UIApplication sharedApplication]delegate];
    if (dele.isNight == 1) {
        self.night.selected = 1;
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //毛玻璃
    self.blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    self.blur.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.blur.alpha = 0.9;
    [self.view addSubview:self.blur];
    
    [self drawSubView];
}


#pragma mark -- 抽屉布局
- (void)drawSubView
{
    UIButton *myCollect = [UIButton buttonWithType:UIButtonTypeSystem];
    myCollect.frame = CGRectMake(0, SAHeight * 130, SAWindth * 100, SAHeight * 30);
    [myCollect setTitle:@"我的收藏" forState:UIControlStateNormal];
    [myCollect setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
//    myCollect.layer.borderWidth = 1;
//    myCollect.layer.cornerRadius = 10;
//    myCollect.layer.borderColor = [UIColor whiteColor].CGColor;
    myCollect.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [myCollect addTarget:self action:@selector(myCollect:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myCollect];
//    myCollect.backgroundColor = [UIColor redColor];
    
    UIButton *myCache = [UIButton buttonWithType:UIButtonTypeSystem];
    myCache.frame = CGRectMake(0, SAHeight * 180, SAWindth * 100, SAHeight * 30);
    [myCache setTitle:@"清理缓存" forState:UIControlStateNormal];
    myCache.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [myCache setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [myCache addTarget:self action:@selector(myCache:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myCache];
    //    myCache.backgroundColor = [UIColor whiteColor];
    
    self.night = [UIButton buttonWithType:UIButtonTypeSystem];
    self.night.frame = CGRectMake(0, SAHeight * 230, SAWindth * 100, SAHeight * 30);
    [self.night setTitle:@"夜间模式" forState:UIControlStateNormal];
    self.night.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [self.night setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [self.night addTarget:self action:@selector(night:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.night];

    UIButton *aboutUs = [UIButton buttonWithType:UIButtonTypeSystem];
    aboutUs.frame = CGRectMake(0, SAHeight * 280, SAWindth * 100, SAHeight * 30);
    [aboutUs setTitle:@"关于我们" forState:UIControlStateNormal];
    aboutUs.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [aboutUs addTarget:self action:@selector(aboutUs:) forControlEvents:UIControlEventTouchUpInside];
    [aboutUs setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [self.view addSubview:aboutUs];
    //    aboutUs.backgroundColor = [UIColor redColor];
    
    
}

/* 我的收藏 */
- (void)myCollect:(UIButton *)button
{
    CollectController *collection = [[CollectController alloc] init];
    //跳转效果
    collection.modalTransitionStyle = 1;
    
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:collection];
    
    [self presentViewController:navi animated:YES completion:^{
        
    }];
}

//清理缓存
- (void)myCache:(UIButton *)button;
{
    NSLog(@"清理缓存");
    
    /*清除缓存,并出现提示框*/
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您确定要清除缓存?" preferredStyle:UIAlertControllerStyleAlert];
    [self.navigationController presentViewController:alert animated:YES completion:^{
        
    }];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        /*删除缓存文件*/
        [alert dismissViewControllerAnimated:YES completion:^{
            
        }];
//        NSString *cachP = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        [self moveCaches];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [alert dismissViewControllerAnimated:YES completion:^{
            
        }];
        
    }];
    [alert addAction:action];
    [alert addAction:cancelAction];
    /* VC 模态推出alert 界面  */
    [self presentViewController:alert animated:YES completion:^{
        
        
    }];
}


#pragma mark - 夜间模式

- (void)night: (UIButton *)button{
    
    AppDelegate *dele = [[UIApplication sharedApplication]delegate];
   
    button.selected = !button.selected;
    if (button.selected == 0) {
        dele.isNight = 0;
        HomeViewController *home = [[HomeViewController alloc]init];
        home =dele.home;
        [home.tableView reloadData];
        Hot_rodViewController *hot = [[Hot_rodViewController alloc]init];
        hot = dele.Hot_rod;
        [hot.tableView reloadData];
        SearchViewController *findCar = [[SearchViewController alloc]init];
        findCar = dele.findCar;
        [findCar.tableView reloadData];
        NSString *docP = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *readStrPath = [docP stringByAppendingPathComponent:@"isNight.txt"];
        NSString *str = [NSString stringWithFormat:@"%d", (int)dele.isNight];
        [str writeToFile:readStrPath atomically:YES];
        
    }
    else{
        AppDelegate *dele = [[UIApplication sharedApplication]delegate];
        dele.isNight = 1;
        HomeViewController *home = [[HomeViewController alloc]init];
        home =dele.home;
        [home.tableView reloadData];
        Hot_rodViewController *hot = [[Hot_rodViewController alloc]init];
        hot = dele.Hot_rod;
        [hot.tableView reloadData];
        SearchViewController *findCar = [[SearchViewController alloc]init];
        findCar = dele.findCar;
        [findCar.tableView reloadData];
        NSString *docP = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *readStrPath = [docP stringByAppendingPathComponent:@"isNight.txt"];
        NSString *str = [NSString stringWithFormat:@"%d", (int)dele.isNight];
        [str writeToFile:readStrPath atomically:YES];
    
    
    }


}
#pragma mark -  清除缓存
- (void)moveCaches
{
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    
    NSString *str = [NSString stringWithFormat:@"缓存已清除%.1fM", [self folderSizeAtPath:path] + [[SDImageCache sharedImageCache] getSize] / (1024 * 1024)];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清除缓存" message:str preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self.sideMenuViewController hideMenuViewController];
    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    
    NSLog(@"%@",str);
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];
    for (NSString *p in files) {
        NSError *error;
        NSString *Path = [path stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:Path]) {
            [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
        }
    }
    
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
    
}

- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
//遍历文件夹获得文件夹大小，返回多少M
- (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}



/* 关于我们 */
- (void)aboutUs:(UIButton *)button;
{
    AboutUs *us = [[AboutUs alloc] init];
    us.modalTransitionStyle = 1;
    
//    [self.navigationController pushViewController:us animated:YES];
    
    
    
    [self presentViewController:us animated:YES completion:^{
        
    }];

}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
